<?php


namespace app\api\controller\v25;


use app\api\controller\v13\Auth2;
use app\common\cachemodel\ZlcrAstr;
use app\common\cachemodel\ZlcrCustomReportGoods;
use app\common\cachemodel\ZlcrCustomReportQuestionInfo;
use app\common\cachemodel\ZlcrTag;
use Exception;
use think\Cache;
use think\response\Json;

/**
 * @version 2.6.0
 * 定制报告相关
 * Class CustomReport
 * @package app\api\controller\v25
 */
class CustomReport extends Auth2
{


    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取定制报告下单示例数据
     * @return Json
     * @throws Exception
     */
    public function getCustomReportPayOrderDemo()
    {
        $gid = input('gid/d', 0);
        if (!$gid)
        {
            return show(0, '请求参数异常');
        }
        $questionGoods = (new ZlcrCustomReportGoods())->getInfoById($gid);
        if (!$questionGoods)
        {
            return show(config('code.error'), '数据不存在');
        }
        $tag = (new ZlcrTag())->getInfoById($questionGoods['tid'], ['tag_name']);
        $astrInfo = (new ZlcrAstr())->getAstrInfoFromCache(10, ['avatar', 'extend']);

        $extendInfos = [];
        if (isset($questionGoods['extend']) && $questionGoods['extend'])
        {
            $extend = \GuzzleHttp\json_decode($questionGoods['extend'], true);

            foreach ($extend['supplement'] as $key => $extendItem)
            {
                $extendInfoItem = [
                    'id' => $key + 1,
                    'title' => $extendItem['title'],
                    'type' => (int)$extendItem['type'],
                    'option_list' => []
                ];
                if ((int)$extendItem['type'] === 2)
                {
                    $optionList = [];
                    if (isset($extendItem['option1']) && $extendItem['option1'])
                    {
                        $optionList[] = [
                            'option' => 1,
                            'content' => $extendItem['option1']
                        ];
                    }
                    if (isset($extendItem['option2']) && $extendItem['option2'])
                    {
                        $optionList[] = [
                            'option' => 2,
                            'content' => $extendItem['option2']
                        ];
                    }
                    if (isset($extendItem['option3']) && $extendItem['option3'])
                    {
                        $optionList[] = [
                            'option' => 3,
                            'content' => $extendItem['option3']
                        ];
                    }
                    if (isset($extendItem['option4']) && $extendItem['option4'])
                    {
                        $optionList[] = [
                            'option' => 4,
                            'content' => $extendItem['option4']
                        ];
                    }
                    $extendInfoItem['option_list'] = $optionList;
                }
                $extendInfos[$extendInfoItem['id']] = $extendInfoItem;
            }
        }

        if ($this->user)
        {
            Cache::set(ZlcrCustomReportQuestionInfo::$supplementInfoKey . $this->user['id'], $extendInfos, 3600 * 5);
        }

        $data = [
            'question_name' => $questionGoods['question_name'],
            'question_desc' => $questionGoods['description'],
            // 'tips' => '注：8:00-16:00的提问，将在接单后6小时内得到回复，16:00-次日8:00的提问，将在次日8:00开始计时，6小时内得到回复',
            'tips' => '',
            'astr_avatar' => $astrInfo ? array_column($astrInfo, 'avatar') : [],//todo 已完成报告的占星师头像
            'astr_desc' => '您的问题将由准了至臻占星团队产出专业定制报告',
            'is_merge' => $questionGoods['is_merge'],
            'question_type' => $questionGoods['qtype'],
            'example_url' => $questionGoods['example_url'],
            'title' => $tag['tag_name'] . '问题',
            'extend_infos' => array_values($extendInfos)
        ];
        return show(200, '请求成功', $data);
    }
}
